package com.kanas.webak.userspace.service;

import javax.inject.Singleton;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kanas.framework.util.Security;
import com.kanas.webak.admin.domain.AppUser;
import com.kanas.webak.admin.repository.UserRepository;
import com.kanas.webak.rbac.service.ServiceException;

@Service("kanas.admin.service.sysindividualservice")
@Singleton
public class UserSpaceServiceImpl implements UserSpaceService{
	@Autowired
	private UserRepository userRepository;

	public UserRepository getUserRepository() {
		return userRepository;
	}

	public void setUserRepository(UserRepository userRepository) {
		this.userRepository = userRepository;
	}
	
	@Transactional
	public String changePsd(String oldPsd, String newPsd,String userId)throws Exception {
		AppUser user=this.userRepository.findOne(userId);
		String oldPassword = Security.digest32HexSHA1(oldPsd + "{" + user.getId() + "}");
		if(!oldPassword.equals(user.getPassword()))throw ServiceException.OLD_PASSWORD_FAILED;
		String newPassword = Security.digest32HexSHA1(newPsd + "{" + user.getId() + "}");
		user.setPassword(newPassword);
		this.userRepository.save(user);
		return user.getId();
	}	
}
